package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000121640712";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCMUcFx6W/RjQz8a4Cl8SmO6CrM5payfiotAof/Oh3ddOz9HXuKjtUF2sxTwuVypAaPVuoiaRlxyZsyaGRjAUYNsUh09nrYSNSu6jecgiaCth9JC51B6kL41nDk28kQaWm758AiZhX3KVzuiSs4zf1O/Q9KiEUvyftTF+1FUpT4n0iUYdOWIEmwj+O7fxNyRrcVI9fvWS7XBK2Tfxf9c2xED0AXGHdeVjx+sy9QsQFJQnBqWCXcIwiVJVM20au5vO8B4GHIL1SC1lTl24Hk5h7rYu7IbQIUTSrIcOkG7e4iBvE97UDu4TEO0M57bIUsFQgQGnaR+tukS5KYqHHFYdBLAgMBAAECggEAQpFql1LqZFxpI7sQXikp0Q0dePWP2n/2HEJOKGv2zR/yaiY5fMNEFjH1q2sTlS5xmkDedJKtqoi9Ys2ZikqJZGeNe0ePgdoTZCPjXBF2mZ4y+2FSKnoraYe6KSiHm4yWx7krMmaB1y69UsCqS5mVqYpuXaqGQCSvYX1BaBjEaanZz/2U8tSADMct5M/OWcdolQ5Id7lSJ74anF+y9yYyQnUppTL1FizD1ynr/W7bJ1cqsqw9NlExVo4jC28q2YUe12JKy/a+7tQmY6d1P/vOmqajvl5GejTjSnPXCIkPUgEd4CHSpC85DYawQlIxnMTeaDyR9Yndp7mmWTy9CPmpQQKBgQDYHmMlvpqMfwQkxOgYTYlIGpYZRiif0J/5N3kj6CK6zxAVSwjEFGxLt0kl1kuPqGBg5VIbTgHv8tuCmw6I8nFy9T/GHil/57+ELLEZbdRNQ8Tfgf6lFmwCfIu4FTiqqJgEf9LDhT13zVWdT4WO+kdXwlbuMz8jgjuBmcXj1XjVYQKBgQCmNoyoF1spnXtrvDBxaQzjOrBIGdsVaOsDTVb0qS+2jGTc96WHuNvJTmcSUYINQ461ZqY3U5dYlJdqFWIjY+/MFCCaOrZoPjflto6Icz2JPfuX7L04dDbLRn4l8ua1lEF3aFVfUVWOri3R578cNfMLXqjxn36KMxRnsPVsne+ZKwKBgC0w7XqPCLIoPS2THxU14Wb6aDiQux3v+gOa3fHsMJbvhWVucz7NJngOs3T+An7froeUbD7ZaP5LwirlUXzBdYecUOGUnQjp5No2njG7DZG6gghanmzW6275rB2qElddOax3LhNEYT4T6bsjTcJAA3sOYC8e+CzsppTLGt4xXTsBAoGAWUxxYVW7Cq6rQoSG2keAPwym7PXHOMd3i/yfPS8wjTj8b6M1caelnY/b3IGiB5JGN7uAjbUBWPvsJXh7k1v7137wzSvCGFtlGKtuBG01oGiGpWbxekUlsyyaVbUcsm2GhqqtQ1qlRtTZlS6XBIcPnwoqZjY0e/dZfSQbl1F7CqUCgYEArq+eoKBgC6Y/lxOFpgASjzxemVb8RCWIrJG711s4LZK1pa/jMxpnSslS6Qt4XaAlOAjzXpLLvILslQw+YClT3j12BkHVALMTiduJRIQHKOl1Aj+Looxve2UqwDDvzRjVeXaQkeJoe86Yhs9VKzGBPAoNECaqa9K/mD+n8VMWm80=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuTlPd+ZtCujFGecyW7zQV13ggUwtW9Ovulm6wu1eKvQrluRVuse1ZsMDSs4e3Bw4KYqeEDekvHITg6OmqI5Wf44b1qVtTmw0O9TOUMd+IAEU1mewW9RxRgapFoRCv9D99NHxFQmdgRYd1m6J0LyJJ9wQVXQ1WBEpcwJYYVPkejxJTjmuDsfRGRb/LQoLokIrgp5bZV38ZM7KZQdaOfe97tp7Mkl3jchD2jG3FTWwVU5iSt/7Wf1NDjex6JIeVyZOjmQXXqzHrs1slmf0Ff1WyQ2rZS4IQdNHVH6ydBWquy1kLofMQ985DhSwONiy9tGf7S50TFaTCsJpeV0mjSEWXwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://lbh.free.svipss.top/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    //收单时间
    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
